package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacharDao;
import com.itheima.edu.info.manager.domain.Teachar;

public class TeacharService {
    private TeacharDao teacharDao=new TeacharDao();

    public boolean isexits(String id) {
        //获取数组
        Teachar[] teachars= teacharDao.findAllTeacher();

        //遍历数组获取每一个元素进行判断
        boolean exists=false;
        for (int i = 0; i < teachars.length; i++) {
            Teachar teachar=teachars[i];
            if(teachar!=null&&teachar.getId().equals(id))
                {
                    exists=true;
                    break;
                }
        }
        return exists;//true 存在 false不存在
    }

    public boolean addTeacher(Teachar t) {
        return  teacharDao.addTeachar(t);
    }

    public Teachar[] findAllTeacher() {
        Teachar[] teachars= teacharDao.findAllTeacher();
        //只要这里面有一个不是null就可以打印，先假设没有，遇到有就结束循环
        boolean flag=false;//没有
        for (int i = 0; i < teachars.length; i++) {
            Teachar teachar=teachars[i];//把元素拿出来看看有没有
            if(teachar!=null) {
                flag=true;
                break;
            }
        }
        if(flag){
            return teachars;
        }else return null;
    }

    public void deleteTeacherById(String id) {
        //业务员把接收到的id拿给库管删除
        teacharDao.deleteTeacherById(id);
    }

    public void updateTeacher(String id, Teachar newTeacher) {
        //拿给库管修改
        teacharDao.updateTeacher(id,newTeacher);
    }
}
